package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.StemealDishMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.nio.file.WatchService;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;

@Service
public class WorkspaceServiceImpl implements WorkspaceService {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    DishMapper dishMapper;
    @Autowired
    StemealDishMapper  stemealDishMapper;
    @Override
    public BusinessDataVO businessData(LocalDate now) {
        //查询今日运营数据
        BigDecimal turnover = orderMapper.sumByCondittion(Orders.COMPLETED, now);
        //有效的订单数据
        Map param = new HashMap();
        param.put("date",now);
        Integer totalOrdeerCount = orderMapper.coutByMap(param);//总订单数
        param.put("status",Orders.COMPLETED);
        Integer validOrderCount = orderMapper.coutByMap(param);//有效订单数
        //新增用户数据
        param.put("newUserDate",now);
        Integer newUsers = userMapper.countByMap(param);
        //计算完成率和平均客单价
        double orderCompletionRate = 0;//订单完成率
        double unitPrice = 0;//平均客单价
        if (validOrderCount!=null && totalOrdeerCount!=null &&turnover!=null &&totalOrdeerCount!=0&&validOrderCount!=0){
            orderCompletionRate = validOrderCount.doubleValue()/totalOrdeerCount;
            unitPrice = turnover.doubleValue()/validOrderCount;

        }
        //封装查询结果
        BusinessDataVO businessDataVO = new BusinessDataVO();
        businessDataVO.setTurnover(turnover.doubleValue());//营业额
        businessDataVO.setValidOrderCount(validOrderCount);//有效订单数据
        businessDataVO.setNewUsers(newUsers);//新增用户数据

        businessDataVO.setOrderCompletionRate(orderCompletionRate);//订单完成率
        businessDataVO.setUnitPrice(unitPrice);//平均客单价


        return businessDataVO;
    }

    @Override
    public OrderOverViewVO overviewOrders(LocalDate now) {
        //查询今日订单数据
        //全部订单
        Map param = new HashMap();
        param.put("date",now);
        Integer allOrders = orderMapper.coutByMap(param);
        //已取消数量
        param.put("status",Orders.CANCELLED);
        Integer cancelledOrders = orderMapper.coutByMap(param);
        //已完成数量
        param.put("status",Orders.COMPLETED);
        Integer completedOrders = orderMapper.coutByMap(param);
        //待派送数量
        param.put("status",Orders.CONFIRMED);
        Integer deliveredOrders = orderMapper.coutByMap(param);
        //待接单数量
        param.put("status",Orders.CONFIRMED);
        Integer waitingOrders = orderMapper.coutByMap(param);

        //封装查询结果
        OrderOverViewVO orderOverViewVO = new OrderOverViewVO();
        orderOverViewVO.setAllOrders(allOrders);
        orderOverViewVO.setCompletedOrders(completedOrders);
        orderOverViewVO.setDeliveredOrders(deliveredOrders);
        orderOverViewVO.setWaitingOrders(waitingOrders);
        orderOverViewVO.setCancelledOrders(cancelledOrders);

        return orderOverViewVO;
    }

    @Override
    public DishOverViewVO overviewDishes() {
        //查询一下起售中的菜品
        Map param = new HashMap();
        param.put("status", StatusConstant.ENABLE);
        Integer startCount = dishMapper.coutByMap(param);
        //查询一下停售的菜品
        param.put("status", StatusConstant.DISABLE);
        Integer stopCount = dishMapper.coutByMap(param);
        //封装返回数据
        DishOverViewVO dishOverViewVO = new DishOverViewVO();
        dishOverViewVO.setDiscontinued(stopCount);
        dishOverViewVO.setSold(startCount);

        return dishOverViewVO ;
    }

    @Override
    public SetmealOverViewVO overviewSetmeals() {
        //查询一下起售中的套餐
        Map param = new HashMap();
        param.put("status",StatusConstant.ENABLE);
       Integer startCount = stemealDishMapper.coutByMap(param);
       //查询一下停售的套餐
        param.put("status",StatusConstant.DISABLE);
        Integer stopCount = stemealDishMapper.coutByMap(param);
        //封装返回数据
        SetmealOverViewVO setmealOverViewVO = new SetmealOverViewVO();
        setmealOverViewVO.setDiscontinued(stopCount);
        setmealOverViewVO.setSold(startCount);
        return setmealOverViewVO;
    }
}
